import React, { useState, useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { addTodo } from '../redux/actions';

// 随机单词库
const WORDS = [
  'React', 'Redux', 'JavaScript', 'TypeScript', 'HTML', 
  'CSS', 'Node', 'Express', 'MongoDB', 'GraphQL',
  'Component', 'State', 'Props', 'Hooks', 'Context',
  'Router', 'API', 'Database', 'Frontend', 'Backend'
];

const AddTodo = () => {
  const [title, setTitle] = useState('');
  const [priority, setPriority] = useState(1);
  const dispatch = useDispatch();

  const generateRandomTitle = () => {
    const wordCount = Math.floor(Math.random() * 3) + 1;
    let result = '';
    for (let i = 0; i < wordCount; i++) {
      const randomIndex = Math.floor(Math.random() * WORDS.length);
      result += (i > 0 ? ' ' : '') + WORDS[randomIndex];
    }
    return result;
  };

  const generateRandomPriority = () => Math.floor(Math.random() * 3) + 1;

  const handleSubmit = (e) => {
    e.preventDefault();
      dispatch(addTodo({
        id: Date.now(),
        title:generateRandomTitle(),
        priority: generateRandomPriority(),
        completed: false
      }));
      setTitle('');
      setPriority(1);
  };


  return (
    <div>
      <form onSubmit={handleSubmit}>
        <button type="submit">Add</button>
      </form>
      
      
    </div>
  );
};

export default AddTodo;